#include "playscene.h"
#include <QDebug>
#include <QLabel>
#include <QMenuBar>
#include <QPainter>
#include <QStatusBar>
#include "custombutton.h"
#include "coin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{
//}
int PlayScene::flag_num = 0;
PlayScene::PlayScene(int level)
{

	this->resize(320, 588);
	this->setWindowIcon(QIcon(":/Coin0001.png"));
	this->setWindowTitle("翻金币");
	this->m_level = level;

	// 设置菜单栏
	QMenuBar * bar = menuBar();
	setMenuBar(bar);
	QMenu * startMenu = bar->addMenu("开始");
	// 添加子项
	QAction * quit = startMenu->addAction("退出");
	// 点击退出
	connect(quit, &QAction::triggered, [=](){
		this->close();
	});

	//添加音效资源
	QSound * music_back = new QSound(":/BackButtonSound.wav",this);
	QSound * music_flip = new QSound(":/ConFlipSound.wav",this);
	QSound * music_win = new QSound(":/LevelWinSound.wav",this);

	// 返回按钮
	CustomButton * back = new CustomButton(":/BackButton.png");
	back->setParent(this);
	back->move(this->width()-back->width(),this->height()-back->height());
	// 点击返回事件
	connect(back,&CustomButton::clicked, [=](){
		// 返回主场景
		qDebug() << "点击返回按钮";
		// 播放音效
		music_back->play();
		// 置金币数量为0
		this->flag_num = 0;
		emit this->back_scene();
	});

	// 放标签控件，显示当前关卡数
	QLabel * level_display = new QLabel;
	level_display->setParent(this);

	// 设置字体
	QFont font;
	font.setFamily("黑体");
	font.setPointSize(12);

	// 格式化文本
	QString str = QString("Level: %1").arg(level);
	// 设置字体和关卡文字显示
	level_display->setFont(font);
	level_display->setText(str);
	level_display->setGeometry(10, this->height()-50,120,50);

	dataConfig config;
	// 初始化关卡金币银币数组
	for (int i=0; i<4; i++)
	{
		for (int j=0; j<4; j++)
		{
			this->gameArray[i][j] = config.mData[level][i][j];
		}
	}

	// 设置到状态栏
//    QStatusBar * stBar = statusBar();
//    stBar->addWidget(level_display);
	QLabel * win = new QLabel;
	win->setParent(this);
	win->setGeometry(0,0,241,84);
	win->setPixmap(QPixmap(":/LevelCompletedDialogBg.png"));
	win->move(40,-100);



	// 显示金币背景图案
	for (int i=0; i < 4; i++)
	{
		for (int j=0; j < 4; j++)
		{
			// 创建背景图片
			QLabel * coin_bg = new QLabel;
			coin_bg->setGeometry(0,0,50,50);
			coin_bg->setPixmap(QPixmap(":/BoardNode(1).png"));
			coin_bg->setParent(this);
			coin_bg->move(57+i*50,200+j*50);

			//判断金币和银币
			QString str;
			if(this->gameArray[i][j] == 1)
				str = ":/Coin0001.png";
			else
				str = ":/Coin0008.png";

			// 创建金币
			Coin * coin = new Coin(str);
			coin->setParent(this);
			coin->move(59+i*50,204+j*50);

			coin->posX = i;
			coin->posY = j;
			coin->flag = this->gameArray[i][j];

			//存储一个金币周围的金币
			around_coin[i][j] = coin;
			if(coin->flag) this->flag_num++;
			qDebug() << this->flag_num;

			// 监听点击反转事件
			connect(coin,&Coin::clicked,[=](){
				//播放音效
				music_flip->play();
				//金币银币翻转
				coin->change_flag();
				if(coin->flag) this->flag_num++;else this->flag_num--;
				this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1:0;

				//延时翻转周围金币
				QTimer::singleShot(800,this,[=](){
					//翻转周围金币
					if(coin->posX+1 < 4) //右侧金币不能越界
					{
						around_coin[coin->posX+1][coin->posY]->change_flag();
						if(around_coin[coin->posX+1][coin->posY]->flag) this->flag_num++;else this->flag_num--;
						this->gameArray[coin->posX+1][coin->posY] = this->gameArray[coin->posX+1][coin->posY] == 0 ? 1:0;
					}
					if(coin->posX-1 >= 0)
					{
						around_coin[coin->posX-1][coin->posY]->change_flag();
						if(around_coin[coin->posX-1][coin->posY]->flag) this->flag_num++;else this->flag_num--;
						this->gameArray[coin->posX-1][coin->posY] = this->gameArray[coin->posX-1][coin->posY] == 0 ? 1:0;
					}
					if(coin->posY+1 < 4)
					{
						around_coin[coin->posX][coin->posY+1]->change_flag();
						if(around_coin[coin->posX][coin->posY+1]->flag) this->flag_num++;else this->flag_num--;
						this->gameArray[coin->posX][coin->posY+1] = this->gameArray[coin->posX][coin->posY+1] == 0 ? 1:0;
					}
					if(coin->posY-1 >= 0)
					{
						around_coin[coin->posX][coin->posY-1]->change_flag();
						if(around_coin[coin->posX][coin->posY-1]->flag) this->flag_num++;else this->flag_num--;
						this->gameArray[coin->posX][coin->posY-1] = this->gameArray[coin->posX][coin->posY-1] == 0 ? 1:0;
					}
					qDebug() <<"金币个数"<< this->flag_num;
					if(this->flag_num == 16)
					{
						qDebug() <<"Victory!";
						music_win->play();
						QPropertyAnimation * animation = new QPropertyAnimation(win,"geometry");
						animation->setDuration(1000);
						animation->setStartValue(QRect(win->x(),win->y(),win->width(),win->height()));
						animation->setEndValue(QRect(win->x(),win->y()+200,win->width(),win->height()));
						animation->setEasingCurve(QEasingCurve::OutBounce);
						animation->start();
					}
				});
			});
		}
	}
}
void PlayScene::paintEvent(QPaintEvent *)
{
	QPainter paint(this);
	QPixmap pix;
	// 设置背景图片
	pix.load(":/PlayLevelSceneBg.png");
	paint.drawPixmap(0,0,pix);

	// 设置标题图片
	pix.load(":/Title.png");
	pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
	paint.drawPixmap(10,30,pix);
}
